package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 查询今日运营数据
     *
     * @return
     */
    @Override
    public BusinessDataVO businessData() {

        LocalDate today = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(today, LocalTime.MAX);
        //新增客户数
        Integer newUsers = reportMapper.countNewUser(begin , end);
        //有效订单数
        Map complete = new HashMap();
        complete.put("begin", begin);
        complete.put("end", end);
        Integer totalOrderCount = reportMapper.countOrder(complete);
        complete.put("status", Orders.COMPLETED);
        Integer validOrderCount = reportMapper.countOrder(complete);
        //订单完成率
        Double orderCompletionRate = 0.0;
        //平均客单价
        Double unitPrice = 0.0;
        //营业额
        Double turnover = 0.0;
        if(reportMapper.getturnover(complete)!=null){
            turnover = reportMapper.getturnover(complete).doubleValue();
        }
        if(totalOrderCount != 0){
            orderCompletionRate = validOrderCount*1.0/totalOrderCount;
            unitPrice = turnover * 1.0 / validOrderCount;
        }
        return BusinessDataVO
                .builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer enable = reportMapper.countSetmeal(StatusConstant.ENABLE);
        Integer disable = reportMapper.countSetmeal(StatusConstant.DISABLE);
        return SetmealOverViewVO
                .builder()
                .discontinued(disable)
                .sold(enable)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        Integer enable = reportMapper.countDish(StatusConstant.ENABLE);
        Integer disable = reportMapper.countDish(StatusConstant.DISABLE);
        return DishOverViewVO
                .builder()
                .discontinued(disable)
                .sold(enable)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        Integer allOrders = reportMapper.countOrder(map);
        map.put("status", Orders.CANCELLED);
        Integer canceledOrders = reportMapper.countOrder(map);
        map.put("status", Orders.COMPLETED);
        Integer completeOrders = reportMapper.countOrder(map);
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = reportMapper.countOrder(map);
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = reportMapper.countOrder(map);
        return OrderOverViewVO
                .builder()
                .deliveredOrders(deliveredOrders)
                .allOrders(allOrders)
                .cancelledOrders(canceledOrders)
                .completedOrders(completeOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
